from PIL import Image, ImageFont, ImageDraw

# A4尺寸参数（300dpi分辨率）
A4_WIDTH = 2480
A4_HEIGHT = 3508


NAMES = [
    # "李艳",
    # "赵晓莹",
    # "廖如茼",
    # "王子文",
    # "尹鹏骁",
    # "赵俊达",
    # "李若灵",
    # "李享宸",
    # "张烜迎",
    # "徐晟睿",
    # "叶丰恺",
    "马钰珍",
    "丁辰",
    "普淏塽",
    # "白云星",
    # "王泽宇",
    # "暨翔宇",
    # "孟子埕",
    # "张宸溪",
    # "任艺颖",
    # "胡语彤",
    # "倪雅宁",
    # "赵奕铭",
    # "申熠辰",
    # "汤修齐",
    # "白珂珂",
    # "张竣铨",
    # "陈子力",
    # "任远之",
    # "张海扬",
    # "王加平",
    # "邓瑞",
    # "李猛",
    # "刘剑涛",
    # "杨智勇",
    # "邓崇文",
    # "陈晶晶",
    # "李丽仙",
    # "闫忠龙",
    # "杨昊程",
    # "刘子剑",
    # "蔡文涛",
    # "卿浩晨",
]


def create_a4_with_smooth_circle(a4_image, output_path, names):
    # 创建白色A4背景
    # 加载字体
    # font = ImageFont.truetype("hyxixiuti.ttf", 60)
    font = ImageFont.truetype("SIMYOU.ttf", 60)

    try:
        # 打开圆形图片并确保有Alpha通道
        gap = 40

        for i in range(4):
            x = gap
            y = 850 * i + gap
            a4_image = draw_badge(a4_image, (x, y), names[i], font)
            # a4_image.paste(overlay, (x, y), overlay)  # 第三个参数是掩膜（mask）

        for i in range(3):
            x = 840
            y = 850 * i + gap + 450

            a4_image = draw_badge(a4_image, (x, y), names[i + 4], font)

        for i in range(4):
            x = 1620
            y = 850 * i + gap

            a4_image = draw_badge(a4_image, (x, y), names[i + 7], font)

        # # 指定文字位置和颜色
        # text_position = (50, 50)
        # text_color = (255, 255, 255)

        # # 创建绘图对象
        # draw = ImageDraw.Draw(a4_image)
        # # 添加文字
        # draw.text(text_position, "Hello, World!", fill=text_color, font=font)

        # 方法1：直接粘贴（自动处理透明边缘抗锯齿）
        # a4_image.paste(overlay, position, overlay)  # 第三个参数是掩膜（mask）

        # 方法2：如果需要进一步优化边缘（可选）
        # a4_image = smooth_edges(a4_image, overlay, position)

        a4_image.save(output_path, dpi=(300, 300))  # 显式设置DPI
        print(f"图片已保存（抗锯齿处理）: {output_path}")
    except Exception as e:
        print(f"错误: {str(e)}")


def draw_badge(a4_image, pos, name, font):
    image_path = "a.png"

    overlay = Image.open(image_path).convert("RGBA")

    a4_image = smooth_edges(a4_image, overlay, pos)

    # 创建绘图对象
    draw = ImageDraw.Draw(a4_image)

    text_position = (pos[0] + 500, pos[1] + 540)
    text_color = (255, 255, 255)

    # 添加文字
    draw.text(text_position, name, fill=text_color, font=font)

    return a4_image


# 可选的高级边缘平滑处理
def smooth_edges(background, overlay, position):
    """更精细的边缘抗锯齿处理"""
    # 创建一个临时画布
    temp = Image.new("RGBA", background.size)
    temp.paste(overlay, position)

    # 将临时画布与原图合成
    background = Image.alpha_composite(background.convert("RGBA"), temp).convert("RGB")

    return background


def chunk_list_with_padding(lst, chunk_size, fill_value=None):
    return [
        lst[i : i + chunk_size]
        + [fill_value] * (chunk_size - len(lst[i : i + chunk_size]))
        for i in range(0, len(lst), chunk_size)
    ]


# 使用示例
if __name__ == "__main__":
    a4_image = Image.new("RGB", (A4_WIDTH, A4_HEIGHT), "white")

    arrs = chunk_list_with_padding(NAMES, 11, "")

    for i, arr in enumerate(arrs):
        create_a4_with_smooth_circle(
            a4_image,
            output_path=f"{i}_output_a4_smooth.png",
            names=arr,
        )
